// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

  @property(cc.Integer)
  m_progMaxLen = 800

  @property(cc.Node)
  m_proImg = null

  @property(cc.Integer)
  m_setWidth = 800

  @property(cc.Integer)
  m_speed = 200

  @property(Boolean)
  m_prochangeFlag = false
  
  @property(Function)
  callbackFunction = null

  onLoad() {
    this.m_proImg.width = 0
  }

  start() {

  }

  // 过场动画
  update(dt) {
    if (this.m_prochangeFlag) {
      if (this.m_proImg.width < this.m_setWidth) {
        this.m_proImg.width += dt * this.m_speed
      }
      if (this.m_proImg.width >= this.m_progMaxLen) {
        this.m_prochangeFlag = false
        if (this.callbackFunction) {
          this.callbackFunction()
        }
      }
    }
  }
  // 设置标志
  ctor() {
    this.m_prochangeFlag = false
  }
  // 设置进度条，桂东进度条整体长度百分比为0-1
  setProgress(pro) {
    if (pro > 1 || pro < 0) {
      return
    }
    let width = this.m_progMaxLen * pro

    // 如果设置宽度比上一次小就返回
    if (width < this.m_setWidth) {
      return
    }
    this.m_setWidth = this.m_progMaxLen * pro
    this.m_prochangeFlag = true
    // this.m_proImg.width = this.m_setWidth
  }

}
